package com.godfrey.controller;

import com.godfrey.pojo.User;
import com.godfrey.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * 控制层
 *
 * @author godfrey
 * @date 2020-07-23
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "保存对象")
    @PostMapping()
    public User saveUser(@ApiParam(value = "用户") @RequestBody User user) {
        return userService.save(user);
    }

    @ApiOperation(value = "通过id删除User")
    @DeleteMapping("/{id}")
    public void deleteUser(@ApiParam(value = "用户id") @PathVariable("id") Integer userId) {
        userService.deleteById(userId);
    }

    @ApiOperation(value = "通过id删除User")
    @PutMapping("/{id}")
    public User updateUser(@ApiParam(value = "用户id") @PathVariable("id") Integer userId, @ApiParam(value = "用户") User user) {
        return userService.updateUser(userId, user);
    }

    @ApiOperation(value = "查询用户信息")
    @GetMapping("{id}")
    public User getUserInfo(@ApiParam(value = "用户id") @PathVariable("id") Integer userId) {
        return userService.getUserInfo(userId);
    }

    @ApiOperation(value = "分页查询用户")
    @GetMapping("/list")
    public Page<User> pageQuery(
            @ApiParam(value = "启始页") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return userService.pageQuery(pageNum, pageSize);
    }
}